package com.dinadi.yidi.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.DesignerWorksRequstEntity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 11:21
 */

public class DesignerWorksAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    Context mContext;
    private List<DesignerWorksRequstEntity> mDatas = new ArrayList<>();
    private String productId;
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }

    public void addAllData(List<DesignerWorksRequstEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public DesignerWorksAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView nick_name, nick_name_price, original_price;
        ImageView imageView;
        RelativeLayout details;

        public ViewHolder(View itemView) {
            super(itemView);
            nick_name = (TextView) itemView.findViewById(R.id.nick_name);
            imageView = (ImageView) itemView.findViewById(R.id.img_product);
            details = (RelativeLayout) itemView.findViewById(R.id.details);
            nick_name_price = (TextView) itemView.findViewById(R.id.nick_name_price);
            original_price = (TextView) itemView.findViewById(R.id.original_price);
        }

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_creative_product, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new DesignerWorksAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DesignerWorksAdapter.ViewHolder itemViewHolder = (DesignerWorksAdapter.ViewHolder) holder;
        itemViewHolder.nick_name.setText(mDatas.get(position).getTitle());
        String price = mDatas.get(position).getPrice();
        String originalPrice = mDatas.get(position).getOriginalPrice();
        itemViewHolder.nick_name_price.setText("￥" + price);
        if (price!= null && !price.equals(originalPrice)) {
            itemViewHolder.original_price.setText("￥" + originalPrice);
            itemViewHolder.original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }else {
            itemViewHolder.original_price.setText("");
        }


        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.imageView);
            }
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.imageView);

        }

        productId = mDatas.get(position).getProductId();

        holder.itemView.setTag(productId);
//        itemViewHolder.details.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, ProductDetailsActivity.class);
//                intent.putExtra("productId", productId);
//                mContext.startActivity(intent);
//            }
//        });
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

}
